//
//  MKRootViewController.m
//  MistKit_Example
//
//  Created by ChenJie on 5/30/20.
//  Copyright © 2020 Mist. All rights reserved.
//

#import "MKRootViewController.h"
#import <MistKit/MistKit.h>

@interface MKRootViewController ()
<
UITableViewDataSource,
UITableViewDelegate
>

@property (nonatomic, strong) NSMutableArray *cells;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation MKRootViewController

#pragma mark - ViewController lifecycle

- (instancetype)init {
    self = [super init];
    if (self) {
        [self buildDataSource];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = NSStringFromClass(self.class);
    
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    
    CGFloat topMark = 0;
    CGSize viewSize = self.view.bounds.size;
    
    self.tableView.frame
    = CGRectMake(0, topMark, viewSize.width, viewSize.height - topMark);
    
}

- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleLightContent;
}

#pragma mark - Public methods



#pragma mark - Private methods

- (void)buildDataSource {
    NSArray *cells = @[
        @{@"class": UITableViewCell.class,
          @"title": @"One",
          @"target": UIViewController.class,
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Two",
          @"target": @"UIViewController",
        },
        @{@"title": @"Three",
        },
        @{@"class": UITableViewCell.class,
          @"title": @"Four",
          @"target": UIViewController.class,
        },
    ];
    
    [self.cells addObjectsFromArray:cells];
}

#pragma mark - Event response methods

- (void)buttonClicked:(UIButton *)button {
    NSLog(@"button clicked...");
}

#pragma mark - Deletate methods



#pragma mark - <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section {
    return self.cells.count;
}

#pragma mark - <UITableViewDelegate>

-       (CGFloat)tableView:(UITableView *)tableView
heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 45;
}

- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *cellInfo = self.cells[indexPath.row];
    
    Class cellClass = cellInfo[@"class"] ?: UITableViewCell.class;
    NSString *cellIdentifier = NSStringFromClass(cellClass);
    
    UITableViewCell *cell =
    [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[cellClass alloc] initWithStyle:UITableViewCellStyleDefault
                                reuseIdentifier:cellIdentifier];
    }
    
    cell.textLabel.text = cellInfo[@"title"];
    
    return cell;
}

-       (void)tableView:(UITableView *)tableView
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *cellInfo = self.cells[indexPath.row];
    
    Class targetVcClass = cellInfo[@"target"] ?: UIViewController.class;
    if ([targetVcClass isKindOfClass:NSString.class]) {
        targetVcClass = NSClassFromString((NSString *)targetVcClass);
    }
    
    UIViewController *targetVc = [[targetVcClass alloc] init];
    if ([targetVc isMemberOfClass:UIViewController.class]) {
        targetVc.view.backgroundColor = [UIColor whiteColor];
        targetVc.navigationItem.title = NSStringFromClass(targetVc.class);
    }
    
    [self.navigationController pushViewController:targetVc animated:YES];
}

#pragma mark - Setter



#pragma mark - Getter

- (NSMutableArray *)cells {
    if (!_cells) {
        NSMutableArray *array = [NSMutableArray arrayWithCapacity:3];
        _cells = array;
    }
    return _cells;
}

- (UITableView *)tableView {
    if (!_tableView) {
        UITableView *tableView =
        [[UITableView alloc] initWithFrame:CGRectZero
                                     style:UITableViewStylePlain];
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        tableView.tableFooterView = [UIView new];
        tableView.estimatedRowHeight = 0;
        
        _tableView = tableView;
    }
    return _tableView;
}

#pragma mark - Memory management

- (void)dealloc {
}

@end
